<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录_音乐相册</title>

  <script src="https://unpkg.com/vue@3"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

  <script>
    axios.defaults.baseURL = "https://<YOUAPPID>.lafyun.com";
  </script>
  <style>
    body {
      background: #f3f3f3;
    }
    #app {
      background: #fff;
      margin: 0 auto;
      padding: 10px;
    }
    #page-title {
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="app">
    <h1 id="page-title">登录</h1>
    <hr>
    <div id="page-title">
      用户名：<input v-model="username" type="text" name="username" />  
      <br/>  
      密码：<input v-model="password" type="password" name="password" />  
      <br/>
      {{ info }}
      <input @click="login()" type="submit" value="登录" />
      <br/>  
      <span>没有账号？<a href="register.html">注册</a></span>
    </div>
    <hr>
  </div>
  <script>
    Vue.createApp({
      data() {  //数据
        return {
          username: "",
          password: "",
          info: ""
        }
      },
      mounted: function () {  // 打开页面时执行
        //判断是否登录
        if (localStorage.getItem("access_token") != null) {
          location.href = "./index.html";
          return;
        }
      },
      methods:{ // 方法
        login: function () {
          let that = this
          axios({ // 请求登录
              method: 'POST',
              url:'/albumapi-user-login',
              data: {
                username: this.username,
                password: this.password,
              }
            })
            .then(res => {
              console.log(res.data)
              if (res.data.code != 200){  //不是200 处理失败
                that.info = res.data.error;
                return;
              }
              //存localStorage
					    localStorage.setItem('access_token', res.data.access_token);
              localStorage.setItem('userid', res.data.user_id);
              localStorage.setItem('username', this.username);

              location.href = "./index.html";
            });
        }
      },

    }).mount('#app');
  </script>
</body>
</html>